/******************************************************************************************************************
* File:ECSConsole.java
* Course: 17655
* Project: Assignment 3
* Copyright: Copyright (c) 2009 Carnegie Mellon University
* Versions:
*	1.0 February 2009 - Initial rewrite of original assignment 3 (ajl).
*
* Description: This class is the console for the museum environmental control system. This process consists of two
* threads. The ECSMonitor object is a thread that is started that is responsible for the monitoring and control of
* the museum environmental systems. The main thread provides a text interface for the user to change the temperature
* and humidity ranges, as well as shut down the system.
*
* Parameters: None
*
* Internal Methods: None
*
******************************************************************************************************************/
package SystemB;

import java.util.LinkedHashMap;

import TermioPackage.*;
import InstrumentationPackage.CommandConsoleOption;
import Alarms.*;

public class SysBConsole
{
	static LinkedHashMap<String, CommandConsoleOption> optionMap;
	static int eventId = 100;
	
	private static void setOptions() {
		optionMap = new LinkedHashMap<String, CommandConsoleOption>();
		
		optionMap.put("1", new CommandConsoleOption("1","Arm",0,""));
		optionMap.put("2", new CommandConsoleOption("2","Disarm\n",0,""));
		optionMap.put("3", new CommandConsoleOption("3","Sprinkler On\n",0,""));
		optionMap.put("4", new CommandConsoleOption("4","Sprinkler Off\n",0,""));
	}

	
	public static void main(String args[])
	{
    	Termio UserInput = new Termio();	// Termio IO Object
		String EvtMgrIP;					// Event Manager IP address
		boolean Done = false;				// Main loop flag
		String Option;						// Menu choice from user
		FireAlarmMonitor Monitor = null;		// The environmental control system monitor
		String[] monitorArgs = new String[0];

		/////////////////////////////////////////////////////////////////////////////////
		// Get the IP address of the event manager
		/////////////////////////////////////////////////////////////////////////////////

   		System.out.println( "\nEnter IP address of event manager or..." );
  		System.out.print( "press enter if on local machine: " );
		EvtMgrIP = UserInput.KeyboardReadString();

		System.out.print("\n\nAttempting to register..." );

		monitorArgs = new String[1];
		monitorArgs[0] = EvtMgrIP;

 		Monitor = new FireAlarmMonitor("Fire Monitor", 700, 0, 198, 550, monitorArgs, 91);

		// Here we check to see if registration worked. If ef is null then the
		// event manager interface was not properly created.

		if (Monitor.isRegistered())
		{
			setOptions();
			
			Monitor.start(); // Here we start the monitoring and control thread

			while (!Done)
			{
				// Here, the main thread continues and provides the main menu

				System.out.println( "\n\nSecurity Control System Command Console: \n" );

				System.out.println( "Select an Option: \n" );
				
				for (String key : optionMap.keySet()) {
					System.out.println(key + ":" + optionMap.get(key).getDescription());
				}
				
				System.out.println( "X: Stop Entire System\n" );
				System.out.print( "\n>>>> " );
				Option = UserInput.KeyboardReadString();

				//////////// option 1 ////////////

				
				if (Option.equalsIgnoreCase( "1" )) {
					Monitor.arm();
				}
				
				//////////// option 2 ////////////

				
				if (Option.equalsIgnoreCase( "2" )) {
					Monitor.disArm();
				}
				
				//////////// option 3 ////////////

				
				if (Option.equalsIgnoreCase( "3" )) {
					Monitor.turnOnSprinkler();
				}
				
				//////////// option 4 ////////////

				
				if (Option.equalsIgnoreCase( "4" )) {
					Monitor.turnOffSprinkler();
				}
				
				//////////// option Q ////////////

				if (Option.equalsIgnoreCase( "Q" ))
				{
					// Here the user is done, so we set the Done flag and halt
					// the environmental control system. The monitor provides a method
					// to do this. Its important to have processes release their queues
					// with the event manager. If these queues are not released these
					// become dead queues and they collect events and will eventually
					// cause problems for the event manager.

					Monitor.Halt();
					Done = true;
					System.out.println( "\nConsole Stopped... Exit monitor mindow to return to command prompt." );
				} // if
				
				
				//////////// option X ////////////

				if (Option.equalsIgnoreCase( "X" ))
				{
					// Here the user is done, so we set the Done flag and halt
					// the environmental control system. The monitor provides a method
					// to do this. Its important to have processes release their queues
					// with the event manager. If these queues are not released these
					// become dead queues and they collect events and will eventually
					// cause problems for the event manager.

					Monitor.CompleteHalt();
					Done = true;
					System.out.println( "\nConsole Stopped... Exit monitor mindow to return to command prompt." );
				} // if

			} // while

		} else {

			System.out.println("\n\nUnable start the monitor.\n\n" );

		} // if

  	} // main

} // ECSConsole
